package com.xx.doublexchat.common.chat.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.xx.doublexchat.common.chat.dao.MessageDao;
import com.xx.doublexchat.common.chat.dao.MessageMarkDao;
import com.xx.doublexchat.common.chat.domain.entity.Message;
import com.xx.doublexchat.common.chat.domain.entity.MessageMark;
import com.xx.doublexchat.common.chat.domain.vo.request.ChatMessageRequest;
import com.xx.doublexchat.common.chat.domain.vo.response.ChatMessageResponse;
import com.xx.doublexchat.common.chat.event.MessageSendEvent;
import com.xx.doublexchat.common.chat.service.ChatService;
import com.xx.doublexchat.common.chat.service.adapter.MessageAdapter;
import com.xx.doublexchat.common.chat.service.strategy.msg.AbstractMsgHandler;
import com.xx.doublexchat.common.chat.service.strategy.msg.MsgHandlerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ChatServiceImpl implements ChatService {

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;

    @Autowired
    private MessageMarkDao messageMarkDao;

    @Autowired
    private MessageDao messageDao;

    /**
     * 发送消息
     * @param request
     * @param uid
     * @return
     */
    @Override
    @Transactional
    public Long sendMsg(ChatMessageRequest request, Long uid) {
        //获取对应消息的策略类
        AbstractMsgHandler<?> msgHandler = MsgHandlerFactory.getStrategyNoNull(request.getMsgType());
        Long msgId = msgHandler.checkAndSaveMsg(request, uid);
        // 发布消息发送事件
        applicationEventPublisher.publishEvent(new MessageSendEvent(this, msgId));
        return msgId;
    }

    /**
     * 获取消息详细信息组合
     * @param message 消息
     * @param receiveUid 接受消息的uid，可null
     * @return
     */
    @Override
    public ChatMessageResponse getMsgResp(Message message, Long receiveUid) {
        return CollUtil.getFirst(getMsgRespBatch(Collections.singletonList(message), receiveUid));
    }

    /**
     * 获取消息详细信息
     * @param msgId
     * @param receiveUid 接受消息的uid，可null
     * @return
     */
    @Override
    public ChatMessageResponse getMsgResp(Long msgId, Long receiveUid) {
        Message msg = messageDao.getById(msgId);
        return getMsgResp(msg, receiveUid);
    }

    /**
     * 获取消息组合
     * @param messages
     * @param receiveUid
     * @return
     */
    public List<ChatMessageResponse> getMsgRespBatch(List<Message> messages, Long receiveUid) {
        if (CollectionUtil.isEmpty(messages)) {
            return new ArrayList<>();
        }
        //查询消息点赞状态信息集合
        List<MessageMark> msgMark = messageMarkDao.getValidMarkByMsgIdBatch(messages.stream().map(Message::getId).collect(Collectors.toList()));
        return MessageAdapter.buildMsgResp(messages, msgMark, receiveUid);
    }
}
